package org.openModule;

import com.scm.all.export.*;
import com.scm.all.struct.TagModuleenTryX64;
import com.scm.all.struct.TagModuleenTryX86;
import com.scm.all.struct.TagProcessenTry32;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import org.openProcess.StageWindowsProcess;
import org.openProcess.StageWindowsProcessAndListView;
import org.window.PaneControllerAndHBoxStateAndTextValues;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

/**
 * 进程列表数据
 */
public class StageWindowsModuleAndListView {
    public static String ModuleAddress;
    public static ListView<String> listView = new ListView();
    /**
     * 存放进程句柄
     */
    public static ObservableList<String> items = FXCollections.observableArrayList ();
    public static void init(){
                listView.setLayoutX(0);
        listView.setLayoutY(0);
        listView.setPrefWidth(780);
        listView.setPrefHeight(460);
    }
    public static void run(){
        items.clear();
        if(StageWindowsProcessAndListView.ProcessIDHandle > 0){
            if(ModuleOperationUtilsJNI.systemIs32()){
                for(TagModuleenTryX86 tagModuleenTryX86 : ProcessAndThreadUtils.processPidToModulex86(StageWindowsProcessAndListView.ProcessIDHandle,null)){
                    items.addAll(tagModuleenTryX86.hModule + "     " + tagModuleenTryX86.szExePath);
                }
            }
            if(ModuleOperationUtilsJNI.systemIs64()){
                for(TagModuleenTryX64 tagModuleenTryX64 : ProcessAndThreadUtils.processPidToModuleX64(StageWindowsProcessAndListView.ProcessIDHandle,null)){
                    items.addAll(tagModuleenTryX64.hModule + "     " + tagModuleenTryX64.szExePath);
                }
            }

        }else {
            SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"请先打开进程","温馨提示",0);
        }
        listView.setItems(items);
        listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getClickCount()==2 && event.getButton().name().equals("PRIMARY")) {
                    setClipboardContents(ModuleAddress);
                    StageWindowsModule.stage.hide();
                }
            }
        });
        listView.getSelectionModel().getSelectedItems().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                ModuleAddress = observable.toString().substring(1,observable.toString().length()-1).split("     ")[0];
                if(StageWindowsModule.stage.isShowing()){
                    StageWindowsModule.stage.setTitle("查看模块地址   HEX：  " +ByteUtils.longToHexString(Long.parseLong(ModuleAddress)).toUpperCase() + "     双击复制模块地址");
                }
//                System.out.println("选择进程：  " + observable.toString().substring(1,observable.toString().length()-1));
            }
        });
    }
    public static void setClipboardContents(String v){
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(v);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }
}
